package com.wuliupai.delivery.wuliupaidelivery.activity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import com.wuliupai.delivery.wuliupaidelivery.App;
import com.wuliupai.delivery.wuliupaidelivery.R;
import com.wuliupai.delivery.wuliupaidelivery.db.CityHistoryBean;
import com.wuliupai.delivery.wuliupaidelivery.db.DBUtil;
import com.wuliupai.delivery.wuliupaidelivery.db.ReadAssets;
import com.wuliupai.delivery.wuliupaidelivery.adapter.PlaceSearchAdapter;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.TextChange;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * Descriptions：手动输入筛选地址页面
 * <p>
 * Author：ChenME
 * Date：2017/1/5
 * Email：ibelieve1210@163.com
 */
@EActivity(R.layout.activity_place_search)
public class PlaceSearchActivity extends AppBaseActivity {

    @Bean(DBUtil.class)
    DBUtil dbUtil;

    @ViewById(R.id.lv_placeSearchResult)
    ListView lv_placeSearchResult;
    @ViewById(R.id.et_keyInput)
    EditText et_keyInput;

    public static final int Result_PlaceSearch = 0x3321;

    private ReadAssets readAssets;
    private SQLiteDatabase db;

    private List<CityHistoryBean> cityList;
    private PlaceSearchAdapter adapter;

    @AfterViews
    void onPageStart() {
        setDefaultTitle(this, "搜索地址");
        et_keyInput.setEnabled(true);
        readAssets = new ReadAssets();
        db = readAssets.openDatabase(this);
        cityList = new ArrayList<>();
        adapter = new PlaceSearchAdapter(this, cityList);
        lv_placeSearchResult.setAdapter(adapter);
        lv_placeSearchResult.setOnItemClickListener((parent, view, position, id) -> {
            CityHistoryBean bean = cityList.get(position);
            Intent intent = new Intent();
            intent.putExtra("province", new String[]{bean.getProvinceCode(), bean.getProvince()});
            intent.putExtra("city", new String[]{bean.getCityCode(), bean.getCity()});
            intent.putExtra("area", new String[]{bean.getAreaCode(), bean.getArea()});
            setResult(Result_PlaceSearch, intent);
            onBackPressed();
        });
    }

    @TextChange(R.id.et_keyInput)
    void onKeyInputChanged() {
        List<CityHistoryBean> temp = dbUtil.queryFitResult(db, et_keyInput.getText().toString().trim());
        cityList.clear();
        cityList.addAll(temp);
        adapter.notifyDataSetChanged();
    }

    @Click(R.id.iv_clearInput)
    void clearInput() {
        et_keyInput.setText("");
        cityList.clear();
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        animUtils.leftOutAnimation(this);
    }

    @Override
    protected void onDestroy() {
        if (null != db) {
            db.close();
        }
        super.onDestroy();
    }
}